home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C++ / Snippets / QD3D Juggler / Juggler Sources / CPropPane.cp < prev    next >
Encoding:
Text File  |  1995-12-27  |  5.6 KB  |  230 lines  |  [TEXT/CWIE]

  1. //
  2. //    CPropPane.cp
  3. //
  4. //    class CPropPane
  5. //    A Pane for rendering 4 boxes in a QuickDraw 3D view.
  6. //    Borrowed heavily from the "START HERE" sample code from Apple.
  7. //
  8. //    by James Jennings
  9. //    November 22, 1995
  10. //
  11.  
  12. #include "CPropPane.h"
  13. #include "C3Lights.h"        // defines the light group
  14. #include "CCameraMaker.h"    // defines the camera
  15. #include "CBoxMaker.h"
  16. #include "CClubMaker.h"
  17. #include "StQ3Disposer.h"
  18. #include "QD3D Debug Macros.h"
  19.  
  20. //const CommandT cmd_NewPropPane = 1200;    // used in CJuggleApp
  21. const CommandT cmd_Jitterbug = 1201;
  22. const CommandT cmd_CubeProp  = 1202;
  23. const CommandT cmd_ClubProp  = 1203;
  24.  
  25.  CPropPane*    
  26.  CPropPane::CreatePropPaneStream(LStream *inStream)
  27.  {
  28.      return new CPropPane(inStream);
  29.  }
  30.  
  31. CPropPane::CPropPane() : mPropType(prop_Cube), mJitterbug(false)
  32. {    // default constructor
  33.     // Does nothing. Must call FinishCreate() later.
  34. }
  35.  
  36. CPropPane::CPropPane(const CPropPane &inOriginal) 
  37.     : CQD3DGimbalPane(inOriginal), mCube(inOriginal.mCube), mClub(inOriginal.mClub),
  38.         mPropType(prop_Cube), mJitterbug(false)
  39. {    // copy constructor
  40.     StartIdling();    // start the periodical
  41. }
  42.  
  43. CPropPane::CPropPane(LStream *inStream)
  44.     : CQD3DGimbalPane(inStream), mPropType(prop_Cube), mJitterbug(false)
  45. {    // stream constructor
  46.     // Does nothing. Must call FinishCreate() later.
  47. }
  48.     
  49. CPropPane::~CPropPane()
  50. {    // destructor
  51. }
  52.  
  53. void
  54. CPropPane::FinishCreateSelf()
  55. {    // Inherit the other initialization.
  56.     CQD3DGimbalPane::FinishCreateSelf();
  57.  
  58.     StartIdling();    // start the periodical
  59. }
  60.  
  61. void
  62. CPropPane::MakeCamera()
  63. {    // Make and add a camera to the current view.
  64.     SDimension16 frameSize;
  65.     GetFrameSize(frameSize);
  66.     CCameraMaker theCamera(frameSize);    // construct a camera
  67.     // Change the default position
  68.     theCamera.SetLocation(0,7,0);
  69.     theCamera.SetUpVector(0,0,1);
  70.     TQ3Status status = ::Q3View_SetCamera(mView, theCamera.Get());
  71.     ThrowIfQ3Fail_(status);
  72. }
  73.  
  74. void
  75. CPropPane::MakeLightGroup()
  76. {
  77.     C3Lights theLights;
  78.     // add the lights to the group
  79.     TQ3Status status = ::Q3View_SetLightGroup(mView, theLights.Get());
  80.     ThrowIfQ3Fail_(status);
  81. }
  82.  
  83. void
  84. CPropPane::MakeModel()
  85. {
  86.     // construct the model(s)
  87.     TQ3Point3D origin = { 0.5, 0.5, 0.5};
  88.     mCube.MakeModel(CBoxMaker(), origin);
  89.     ::Q3Point3D_Set(&origin, 0.0, 0.0, 1.4);
  90.     mClub.MakeModel(CClubMaker(), origin);
  91.     // Note: We aren't using the member CQD3DPane::mModel
  92. }
  93.  
  94. TQ3Status
  95. CPropPane::SubmitScene()
  96. {    // Submit the stuff we have in our scene.
  97.     TQ3Status status;
  98.     status = ::Q3Style_Submit( mInterpolation, mView );
  99.     if (status==kQ3Failure) return status;
  100.     status = ::Q3Style_Submit( mBackFacing, mView );
  101.     if (status==kQ3Failure) return status;
  102.     status = ::Q3Style_Submit( mFillStyle, mView );
  103.     if (status==kQ3Failure) return status;
  104.     switch (mPropType) {
  105.     case prop_Club:
  106.         status = ::Q3DisplayGroup_Submit( mClub.Get(), mView );
  107.         break;
  108.     case prop_Cube:
  109.     default:
  110.         status = ::Q3DisplayGroup_Submit( mCube.Get(), mView );
  111.         break;
  112.     }
  113.     return status;
  114. }
  115.  
  116. void
  117. CPropPane::SpendTime( const EventRecord &inMacEvent)
  118. {    // on each null event, spin a little.
  119.     CProp *theProp;
  120.     switch (mPropType) {
  121.     case prop_Club: theProp = &mClub; break;
  122.     case prop_Cube:
  123.     default:        theProp = &mCube; break;
  124.     }
  125.     // Set the angle to spin at 90 degrees / second
  126.     float angle = ((float)TickCount()) * kQ3Pi / 120;
  127. //    theProp->SetSpinAngle(angle);
  128.     theProp->SetSpinAngle(angle*3);    // temporary: speed it up a little
  129.     
  130.     // move the cube about on a sphere
  131.     TQ3SphericalPoint sPt;
  132.     TQ3Point3D a, b;
  133.     ::Q3Point3D_Set(&a, 2.0, 0, 0);
  134.     if (mJitterbug) {
  135.         float theta = angle/5;
  136.         b.x = a.x*cos(theta) - a.y*sin(theta);
  137.         b.y = a.x*sin(theta) + a.y*cos(theta);
  138.         b.z = a.z;
  139.         float phi = angle/1.41;
  140.         a.z = b.z*cos(phi) - b.y*sin(phi);
  141.         a.y = b.z*sin(phi) + b.y*cos(phi);
  142.         a.x = b.x;
  143.     }
  144.     theProp->SetPosition(a);
  145.     
  146.     // give it a 23 deg wobble
  147.     const float wobbleSize = 23*kQ3Pi/180;
  148.     theProp->SetTiltAngle( wobbleSize * sin(angle/3) );
  149.     
  150.     // If this is a null event, Refresh().
  151.     // Otherwise, we're in a click-drag loop, and we don't.
  152.     if (inMacEvent.what==nullEvent)
  153.         Refresh();
  154. }
  155.  
  156. Boolean
  157. CPropPane::ClickLoop( const EventRecord &inMacEvent )
  158. {    // subclasses may override to modify the scene in some other way
  159.     SpendTime(inMacEvent);
  160.     return true;
  161. }
  162.  
  163. // ---------------------------------------------------------------------------
  164. //        • ObeyCommand
  165. // ---------------------------------------------------------------------------
  166. //    Respond to commands
  167.  
  168. Boolean
  169. CPropPane::ObeyCommand(
  170.     CommandT    inCommand,
  171.     void        *ioParam)
  172. {
  173.     Boolean        cmdHandled = true;
  174.  
  175.     switch (inCommand) {
  176.         case cmd_Jitterbug:
  177.             mJitterbug = ! mJitterbug;
  178.             break;
  179.         case cmd_CubeProp:
  180.             mPropType = prop_Cube;
  181.             break;
  182.         case cmd_ClubProp:
  183.             mPropType = prop_Club;
  184.             break;
  185.         default:
  186.             cmdHandled = LCommander::ObeyCommand(inCommand, ioParam);
  187.             break;
  188.     }
  189.     
  190.     return cmdHandled;
  191. }
  192.  
  193. // ---------------------------------------------------------------------------
  194. //        • FindCommandStatus
  195. // ---------------------------------------------------------------------------
  196. //    This function enables menu commands.
  197. //
  198.  
  199. void
  200. CPropPane::FindCommandStatus(
  201.     CommandT    inCommand,
  202.     Boolean        &outEnabled,
  203.     Boolean        &outUsesMark,
  204.     Char16        &outMark,
  205.     Str255        outName)
  206. {
  207.  
  208.     switch (inCommand) {
  209.         case cmd_Jitterbug:
  210.             outEnabled = true;
  211.             outUsesMark = true;
  212.             outMark = (mJitterbug)? checkMark : noMark;
  213.             break;
  214.         case cmd_CubeProp:
  215.             outEnabled = true;
  216.             outUsesMark = true;
  217.             outMark = (mPropType==prop_Cube)? checkMark : noMark;
  218.             break;
  219.         case cmd_ClubProp:
  220.             outEnabled = true;
  221.             outUsesMark = true;
  222.             outMark = (mPropType==prop_Club)? checkMark : noMark;
  223.             break;
  224.         default:
  225.             LCommander::FindCommandStatus(inCommand, outEnabled,
  226.                                                 outUsesMark, outMark, outName);
  227.             break;
  228.     }
  229. }
  230.